//
//  UserCollectionViewController.m
//  Arts1
//
//  Created by apple on 15/8/29.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "UserCollectionViewController.h"
#import "UserSectionOneCell.h"
#import "UserSectionTwoCell.h"

#import "AllHeader.h"

//添加/取消收藏图片
//#define kMypictureURL @"http://arts1.dlab.com.cn/app/mypicture"
#define kMypictureURL @"http://arts1.cn/app/mypicture"

//收藏图片列表接口
//#define kMycollectphotoURL @"http://arts1.dlab.com.cn/app/mycollectphoto"
#define kMycollectphotoURL @"http://arts1.cn/app/mycollectphoto"

@interface UserCollectionViewController ()<UICollectionViewDelegateFlowLayout>

//第一个分区获取数据源
@property (nonatomic ,strong) NSMutableArray *dataSource;

//第二个分区获取数据源
@property (nonatomic ,strong) NSMutableArray *dataSourceArr;

@end

@implementation UserCollectionViewController

static NSString * const SectionOneCell = @"SectionOneCell";

static NSString * const SectionTwoCell = @"SectionTwoCell";

#pragma mark - 懒加载
- (NSMutableArray *)dataSource
{
    if (_dataSource == nil)
    {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSMutableArray *)dataSourceArr
{
    if (_dataSourceArr == nil)
    {
        self.dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}


//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
//{
//    if (viewController == self) {
//        [self.collectionView reloadData];
//    }
//}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.navigationController.delegate = self;
    self.collectionView.backgroundColor = kBackgroundColor;

    //注册类
    [self.collectionView registerClass:[UserSectionOneCell class] forCellWithReuseIdentifier:SectionOneCell];
    
    [self.collectionView registerClass:[UserSectionTwoCell class] forCellWithReuseIdentifier:SectionTwoCell];
    
    //添加长按手势
    UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(getPicture:)];
    longPressGR.minimumPressDuration = 1.0;
    [self.collectionView addGestureRecognizer:longPressGR];
    
    //请求数据
    [self requestData];
    
    //请求数据
    [self requestSectionTwo];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(reLoadUserCollectionView) name:@"reLoadUserCollectionView" object:nil];
    
}

- (void)reLoadUserCollectionView
{
//    [self requestData];
//    [self requestSectionTwo];
    [self.collectionView reloadData];
}

/**
 *  第一个分区请求数据
 */
- (void)requestData
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //获取uid和key
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    
    NSString *uid = dic[@"data"][@"uid"];
    NSString *key = dic[@"data"][@"key"];
    NSDictionary *pardic = [NSDictionary dictionary];
    
    
    if (uid && key)
    {
        pardic = @{@"uid":uid,
                   @"key":key};
    }

    [manage POST:kMypictureURL parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        if (responseObject)
        {
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            int status = [dic[@"status"] intValue];
            
            
            if (status == 1)
            {
                _dataSource = nil;
                NSArray *list = dic[@"list"];
                
                for (NSDictionary *dict in list)
                {
                    MypictureModel *model = [[MypictureModel alloc] init];
                    [model setValuesForKeysWithDictionary:dict];
                    [self.dataSource addObject:model];
                }
            }
        }
        //刷新数据
        [self.collectionView reloadData];
            // 请求成功的时候调用这个block
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             // 请求失败的时候调用调用这个block
             NSLog(@"请求失败");
        }];
}

/**
 *  第二个分区请求数据
 */
- (void)requestSectionTwo
{
    
    
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //获取uid和key
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    
    NSString *uid = dic[@"data"][@"uid"];
    NSString *key = dic[@"data"][@"key"];
    NSDictionary *pardic = [NSDictionary dictionary];
    
    
    if (uid && key)
    {
        pardic = @{@"uid":uid,
                   @"key":key};
    }
    
    [manage POST:kMycollectphotoURL parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             if (responseObject) {
                 NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
                 int status = [dic[@"status"] intValue];
//                 [self.dataSourceArr removeAllObjects];
                 if (status == 1) {
                     NSArray *list = dic[@"list"];
                     for (NSDictionary *dict in list){
                         MyCollectPhotoModel *model = [[MyCollectPhotoModel alloc] init];
                         [model setValuesForKeysWithDictionary:dict];
                         [self.dataSourceArr addObject:model];
                     }
                 }
             }
             //刷新数据
             [self.collectionView reloadData];
             // 请求成功的时候调用这个block
             NSLog(@"数据请求数据请求1111-----------%@", dic);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             // 请求失败的时候调用调用这个block
             NSLog(@"请求失败");
         }];
}

#pragma mark <UICollectionViewDataSource>

/**
 *  设置分区的个数
 *
 *  @param collectionView collectionView
 *
 *  @return 返回分区的个数
 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

/**
 *  设置分区的行数
 *
 *  @param collectionView collectionView
 *  @param section        分区
 *
 *  @return 返回每个分区的行数
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else
    {
       return self.dataSourceArr.count;
    }
    
}
/**
 *  重绘cell
 *
 *  @param collectionView collectionView
 *  @param indexPath      indexPath
 *
 *  @return UICollectionViewCell
 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        UserSectionOneCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:SectionOneCell forIndexPath:indexPath];
        MypictureModel *model = _dataSource[indexPath.row];
        cell.model = model;
        cell.array = _dataSource;
        
        return cell;
    }
    else
    {
        UserSectionTwoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:SectionTwoCell forIndexPath:indexPath];
        MyCollectPhotoModel *model = _dataSourceArr[indexPath.row];
        cell.model = model;
        return cell;
    }
}
/**
 *  设置cell上下左右的间距
 *
 *  @param collectionView       collectionView
 *  @param collectionViewLayout collectionViewLayout
 *  @param section              分区
 *
 *  @return UIEdgeInsets
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0)
    {
        return UIEdgeInsetsMake(10, 10, 5, 10);
    }
    return UIEdgeInsetsMake(5, 10, 5, 10);
  
}
/**
 *  设置cell的大小
 *
 *  @param collectionView       collectionView
 *  @param collectionViewLayout collectionViewLayout
 *  @param indexPath            indexPath
 *
 *  @return 返回cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return CGSizeMake(SCREEN_WIDTH - 20, 200 * HEIGHT);
    }
    else
    {
        return CGSizeMake((SCREEN_WIDTH - 30) / 2, 240 * HEIGHT);
    }
}

#pragma mark <UICollectionViewDelegate>
/**
 *  点击cell执行的方法
 *
 *  @param collectionView collectionView
 *  @param indexPath      indexPath
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (_dataSource.count != 0)
        {
            MypictureModel *model = _dataSource[indexPath.row];
            //跳转到详情
            _pushArtView(model);
        }
        
    }
    else
    {
        if (_pushPhotoView)
        {
            MyCollectPhotoModel *model = _dataSourceArr[indexPath.row];
            _pushPhotoView(model);
        }
    }
}
/**
 *  长按手势触发方法
 *
 *  @param getPicture getPicture
 */
- (void)getPicture:(UILongPressGestureRecognizer *)getPicture
{
    if (getPicture.state == UIGestureRecognizerStateBegan)
    {
        //实现只有第一个分区才能有长按手势
        CGPoint point = [getPicture locationInView:self.collectionView];
        NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
        if (indexPath.section == 0)
        {
            //调用相册或相机页面
            _changeFrameBlock();
        }
    }
}

@end
